//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.bbs;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.packet.PacketBbsThread;
import tv.dyndns.kishibe.client.ui.PageSelectable;
import tv.dyndns.kishibe.client.ui.WidgetPageSelector;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PanelBbs extends VerticalPanel implements PageSelectable {
	private final PanelBbs instance = this;
	private final VerticalPanel displayPanel = new VerticalPanel();
	private final PanelBuildThread buildThread = new PanelBuildThread(this);

	public PanelBbs() {
		setWidth("800px");
		add(new HTML("<b>掲示板</b>"));
		displayPanel.setHorizontalAlignment(ALIGN_CENTER);
		reload();
	}

	public void selectPage(int page) {
		Service.Util.getInstance().getBbsThread(page * Constant.BBS_THREADS_PER_PAGE, Constant.BBS_THREADS_PER_PAGE, callbackBbsThread);
	}

	public void reload() {
		Service.Util.getInstance().getNumberOfBbsThreads(callbackGetNumberOfBbsThreads);
	}

	private final AsyncCallback<PacketBbsThread[]> callbackBbsThread = new AsyncCallback<PacketBbsThread[]>() {
		public void onSuccess(PacketBbsThread[] result) {
			displayPanel.clear();

			for (int i = 0; i < result.length; ++i) {
				final PacketBbsThread thread = result[i];
				final DecoratorPanel decoratorPanel = new DecoratorPanel();
				decoratorPanel.setWidget(new PanelThread((int) thread.id, thread.title));
				displayPanel.add(decoratorPanel);
			}
		}

		public void onFailure(Throwable caught) {
		}
	};

	private final AsyncCallback<Integer> callbackGetNumberOfBbsThreads = new AsyncCallback<Integer>() {
		public void onSuccess(Integer result) {
			clear();

			add(new HTML("<b>掲示板</b>"));

			final int numberOfBbsThreads = result;

			final WidgetPageSelector widgetPageSelector = new WidgetPageSelector(instance, (numberOfBbsThreads - 1) / Constant.BBS_THREADS_PER_PAGE + 1);
			add(widgetPageSelector);
			widgetPageSelector.setPage(0);
			setCellHorizontalAlignment(widgetPageSelector, ALIGN_CENTER);

			add(displayPanel);

			add(buildThread);
		}

		public void onFailure(Throwable caught) {
			caught.printStackTrace();
		}
	};
}
